AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )	
AddCSLuaFile( "hud.lua" )
AddCSLuaFile("vgui.lua")
include( 'shared.lua' )
include("player.lua")
function GM:AcceptStream( pl, handler, id )
 
    return true; // Allow all streams
 
end

//function PlayerUse( activator, entity )	
//end
//hook.Add( "PlayerUse", "PlayerUse", PlayerUse )


local function PlayerSpawnProp(ply)
	
end
hook.Add( "PlayerSpawnProp", "PlayerSpawnProp", PlayerSpawnProp )

function playerSpawnedProp(ply, model, ent)
local cost=math.Round(ent:GetPhysicsObject():GetMass()/4)
	if (player_cash[ply:UserID()]>=cost) then 	
	local steam_id =string.Explode(":",ply:SteamID())
	steam_id=steam_id[3]
	table.insert(player_cash,ply:UserID(),player_cash[ply:UserID()]-cost)
	file.Write("base_combat/accounts/id" ..steam_id.. ".txt",player_cash[ply:UserID()] ..",");
	umsg.Start("set_cash", ply);
	umsg.Long(player_cash[ply:UserID()]);
	umsg.End();
	umsg.Start("chat_buy",ply);
	umsg.String("You bought a prop for $"..cost)
	umsg.End();
	 spd[ent:EntIndex()] = ent:GetPhysicsObject():GetMass()
	print("storing mass")
	return true
	else
	umsg.Start("chat_buy",ply);
	umsg.String("You need $"..cost.." for this prop");
	umsg.End();
	ent:Remove()
	end
end
hook.Add("PlayerSpawnedProp", "playerSpawnedProp", playerSpawnedProp)


local function PlayerSpawnSENT(ply,sent)
	if sent=="money_printer" then 
		if player_cash[ply:UserID()]>=1000 then 
		table.insert(player_cash,ply:UserID(),player_cash[ply:UserID()]-1000)
		umsg.Start("set_cash", ply);
		umsg.Long(player_cash[ply:UserID()]);
		umsg.End();
		umsg.Start("chat_buy",ply);
		umsg.String("You Bought a money printer for $1000");
		umsg.End();
		return true
		else 
		umsg.Start("chat_buy",ply);
		umsg.String("You need $1000 for a money printer");
		umsg.End();
		return false 
		end 
	end
end
hook.Add( "PlayerSpawnSENT", "PlayerSpawnSENT", PlayerSpawnSENT )

function SpawnedSENT(ply,ent)
spd[ent:EntIndex()] = 500
end

hook.Add( "PlayerSpawnedSENT", "playerSpawnedSENT", SpawnedSENT );

local function PlayerGiveSWEP(ply)
		if (player_cash[ply:UserID()]>=100) then 
	local steam_id =string.Explode(":",ply:SteamID())
	steam_id=steam_id[3]
	table.insert(player_cash,ply:UserID(),player_cash[ply:UserID()]-100)
	file.Write("base_combat/accounts/id" ..steam_id.. ".txt",player_cash[ply:UserID()] ..",");
	umsg.Start("set_cash", ply);
	umsg.Long(player_cash[ply:UserID()]);
	umsg.End();
	umsg.Start("chat_buy",ply);
	umsg.String("You bought a Gun for $100")

	umsg.End();
	return true
	else
	umsg.Start("chat_buy",ply);
	umsg.String("You need $100 for a Gun");
	umsg.End();
	return false
	end
end
hook.Add( "PlayerGiveSWEP", "PlayerGiveSWEP", PlayerGiveSWEP )

local function PlayerSpawnNPC()
	return false
end
hook.Add( "PlayerSpawnNPC", "PlayerSpawnNPC", PlayerSpawnNPC )

local function PlayerSpawnVehicle(ply)
print(Color(255,0,0,255))
	if (player_cash[ply:UserID()]>=4000) then 
	local steam_id =string.Explode(":",ply:SteamID())
	steam_id=steam_id[3]
	table.insert(player_cash,ply:UserID(),player_cash[ply:UserID()]-4000)
	file.Write("base_combat/accounts/id" ..steam_id.. ".txt",player_cash[ply:UserID()] ..",");
	umsg.Start("set_cash", ply);
	umsg.Long(player_cash[ply:UserID()]);
	umsg.End();
	umsg.Start("chat_buy",ply);
	umsg.String("You bought a Car for $4000")
	umsg.End();
	return true
	else
	umsg.Start("chat_buy",ply);
	umsg.String("You need $4000 for a car");
	umsg.End();
	return false
	end	
end
hook.Add( "PlayerSpawnVehicle", "PlayerSpawnVehicle", PlayerSpawnVehicle)

local function PlayerSpawnEffect()
	return false
end
hook.Add( "PlayerSpawnEffect", "PlayerSpawnEffect", PlayerSpawnEffect )

local function PlayerSpawnRagdoll()
	return false
end
hook.Add( "PlayerSpawnRagdoll", "PlayerSpawnRagdoll", PlayerSpawnRagdoll )


function UseTool( pl, tr, toolmode )
return false
end
hook.Add( "CanTool", "UseTool", UseTool );




spd = spd or {}

if !ConVarExists("spd") then
    CreateConVar("spd", '0', FCVAR_NOTIFY)
end

if !ConVarExists("spd_fire") then
    CreateConVar("spd_fire", '2', FCVAR_NOTIFY)
end

hook.Add("EntityRemoved", "spdEntityRemoved", function(ent)
    if ent:IsValid() and
    ent:GetClass() == "prop_physics" and
    ent:Health() == 0 then
        spd[ent:EntIndex()] = nil
    end
end)

hook.Add("EntityTakeDamage", "spdEntityTakeDamage", function(ent, inflictor, attacker, amount)
print("damage taken")
    if ent:IsValid() and spd[ent:EntIndex()] then
	print("isvalid")
        spd[ent:EntIndex()] = spd[ent:EntIndex()] - amount / 2
		local mass = ent:GetPhysicsObject():GetMass()
		local colmod = spd[ent:EntIndex()] / mass
        ent:SetColor(255, colmod * 255, colmod * 255, 255)
print("collor")

        if math.random(1, 100) < GetConVarNumber("spd_fire") then
            ent:Ignite(10, 20)
        end

        if spd[ent:EntIndex()] < mass * 0.5 then
            ent:GetPhysicsObject():EnableMotion(true)
        end

        if spd[ent:EntIndex()] < mass * 0.25 and
        ent:IsConstrained() then
            local effect = EffectData()
            effect:SetStart(ent:GetPos())
            effect:SetOrigin(ent:GetPos()+ Vector(0, 0, 10))
            effect:SetScale(mass)
            util.Effect("cball_explode", effect)
            constraint.RemoveAll(ent)
        end

        if spd[ent:EntIndex()] < 1 then
			local effect = EffectData()
			effect:SetStart(ent:GetPos())
			effect:SetOrigin(ent:GetPos()+ Vector(0, 0, 10))
			effect:SetScale(mass)
			util.Effect("Explosion", effect)
			ent:Remove()
        end
    end
end)

function OnEntityCreated(ent)
print(ent:GetClass())
print(ent:GetName())

end
hook.Add("OnEntityCreated", "spdOnEntityCreated",OnEntityCreated)

function PlayerNoClip(ply)
return ply:IsAdmin()
end 

hook.Add("PlayerNoClip", "PlayerNoClip", PlayerNoClip)